import requests,bs4
from bs4 import BeautifulSoup
url = r'https://python123.io/ws/demo.html'
head = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2'
}

def getHtmlText(url):#爬取网页内容
    try:
        r = requests.get(url)
        r. encoding = r.apparent_encoding
        return r.text
    except:
        return ''

def fillUnivList(ulist,html):
    soup = BeautifulSoup(html,'html.parser')
    for tr in soup.tbody.children:
        if isinstance(tr,bs4.element.Tag):
            tds = tr('td')
            ulist.append([tds[0].string,tds[1].string,tds[2].string])

def printUnivList(ulist,num):
    print(f'排名\t\t\t学校名称\t\t\t省市')
    for i in range(num):
        u = ulist[i]
        print(f'{u[0]}\t\t\t{u[1]}\t\t\t{u[2]}')



if __name__ == '__main__':
    uinfo = []
    url = 'http://www.zuihaodaxue.com/zuihaodaxuepaiming2019.html'
    num = int(input("输入一个数字:"))
    html = getHtmlText(url)
    fillUnivList(uinfo,html)
    printUnivList(uinfo,num)